package export

import (
	"encoding/csv"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/gogap/logs"
	"github.com/shopspring/decimal"
	"github.com/tealeg/xlsx"
	"log"
	"os"
	"reflect"
	"strconv"
	"strings"
	"sync"
	"time"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/resphelper/placeresp"
	"yundian/internel/app/models/agentincome"
	"yundian/internel/app/models/cashlog"
	"yundian/internel/app/models/devices"
	"yundian/internel/app/models/liveservicelog"
	"yundian/internel/app/models/orders"
	"yundian/internel/app/models/placeincome"
	"yundian/internel/app/models/placemodel"
	"yundian/internel/app/models/refunds"
	"yundian/internel/app/models/reports"
	"yundian/internel/app/models/scanlogs"
	"yundian/internel/app/models/statistics"
	"yundian/internel/app/models/users"
	"yundian/internel/pkg/dingo/global"
	"yundian/internel/pkg/wechatpay"
)

var wg sync.WaitGroup

func NewRow(row *xlsx.Row, data []interface{}) *XlsxRow {
	return &XlsxRow{
		Row:  row,
		Data: data,
	}
}

type XlsxRow struct {
	Row  *xlsx.Row
	Data []interface{}
}

func (row *XlsxRow) SetRowTitle() error {
	return generateRow(row.Row, row.Data)
}

func (row *XlsxRow) GenerateRow() error {
	return generateRow(row.Row, row.Data)
}

func generateRow(row *xlsx.Row, rowStr []interface{}) error {
	if rowStr == nil {
		return errors.New("no data to generate xlsx!")
	}
	for _, v := range rowStr {
		cell := row.AddCell()

		typeStr := reflect.TypeOf(v)
		if typeStr.String() == "string" {
			cell.SetString(v.(string))
		}
		if typeStr.String() == "float64" {
			cell.SetFloat(v.(float64))
		}
		if typeStr.String() == "int" {
			cell.SetInt(v.(int))
		}

		if typeStr.String() == "int64" {
			cell.SetInt64(v.(int64))
		}

	}
	return nil
}

//订单导出

//func OrderDataWriteTplExcel(data []*orders.OrderExportModel, templateSheetName, templateFile, newFileName, index string) (bool, error) {
//	//templateFile := "./static/excel_template/template_order.xls"
//	exportFile := newFileName
//
//	xlsNew := excelize.NewFile()
//
//	excelSheet := xlsNew.NewSheet(templateSheetName)
//
//	//获取模板数据
//	axis := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
//	template, err := excelize.OpenFile(templateFile)
//	if err != nil {
//		log.Fatal(err)
//	}
//	rows := template.GetRows(templateSheetName)
//	fmt.Println(rows)
//	for i, v := range rows[0] {
//		if index == "2" {
//			xlsNew.SetCellValue(templateSheetName, axis[i]+strconv.Itoa(1), v)
//		} else {
//			if v == "是否奖励" {
//				continue
//			}
//			xlsNew.SetCellValue(templateSheetName, axis[i]+strconv.Itoa(1), v)
//
//		}
//	}
//
//	//第3行开始
//	var writingRow = 2
//	//var writeErr error
//
//	if index == "2" {
//		for _, d := range data {
//			//设置行高
//
//			xlsNew.SetRowHeight(templateSheetName, writingRow, 20)
//			//订单编号
//			xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.TradeNo)
//
//			//第三方订单编号
//			xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.OutTradeNo)
//			var statusText = ""
//			//订单状态
//			switch d.State {
//			case 1:
//				statusText = "等待下单"
//			case 2:
//				statusText = "使用中"
//			case 3:
//				statusText = "已撤销"
//			case 4:
//				statusText = "交易失败"
//			case 5:
//				statusText = "已完成"
//			case 6:
//				statusText = "支付超时"
//			case 7:
//				statusText = "待守约"
//			case 10:
//				statusText = "已删除"
//
//			}
//
//			xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), statusText)
//			//会员名称
//			xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.MemberName)
//
//			//订单来源
//			var orderFromStr = ""
//			if d.OrderFrom == "weixin" {
//				orderFromStr = "微信"
//			} else {
//				orderFromStr = "支付宝"
//			}
//
//			xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), orderFromStr)
//
//			//商户名称
//			xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), d.PlaceName)
//
//			//设备标识
//			xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), d.DeviceSn)
//			//支付金额
//			xlsNew.SetCellValue(templateSheetName, "H"+strconv.Itoa(writingRow), d.PayMoney/100)
//			//结算金额
//			xlsNew.SetCellValue(templateSheetName, "I"+strconv.Itoa(writingRow), d.SettlementMoney/100)
//			//服务状态
//			xlsNew.SetCellValue(templateSheetName, "J"+strconv.Itoa(writingRow), d.TradeStateDesc)
//
//			//借出时间
//			xlsNew.SetCellValue(templateSheetName, "K"+strconv.Itoa(writingRow), time.Unix(int64(d.CreateAt), 0).Format("2006-01-02 15:04"))
//			//归还时间
//			xlsNew.SetCellValue(templateSheetName, "L"+strconv.Itoa(writingRow), time.Unix(int64(d.EndAt), 0).Format("2006-01-02 15:04"))
//
//			var IsDrawText = ""
//			if d.IsDraw == 1 {
//				IsDrawText = "是"
//			} else {
//				IsDrawText = "否"
//			}
//
//			//是否奖励
//			xlsNew.SetCellValue(templateSheetName, "M"+strconv.Itoa(writingRow), IsDrawText)
//			writingRow++
//
//		}
//	} else {
//		for _, d := range data {
//			//设置行高
//
//			xlsNew.SetRowHeight(templateSheetName, writingRow, 20)
//			//订单编号
//			xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.TradeNo)
//
//			//第三方订单编号
//			xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.OutTradeNo)
//			var statusText = ""
//			//订单状态
//			switch d.State {
//			case 1:
//				statusText = "等待下单"
//			case 2:
//				statusText = "使用中"
//			case 3:
//				statusText = "已撤销"
//			case 4:
//				statusText = "交易失败"
//			case 5:
//				statusText = "已完成"
//			case 6:
//				statusText = "支付超时"
//			case 7:
//				statusText = "待守约"
//			case 10:
//				statusText = "已删除"
//
//			}
//
//			xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), statusText)
//			//会员名称
//			xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.MemberName)
//
//			//订单来源
//			var orderFromStr = ""
//			if d.OrderFrom == "weixin" {
//				orderFromStr = "微信"
//			} else {
//				orderFromStr = "支付宝"
//			}
//
//			xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), orderFromStr)
//
//			//商户名称
//			xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), d.PlaceName)
//
//			//设备标识
//			xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), d.DeviceSn)
//			//支付金额
//			xlsNew.SetCellValue(templateSheetName, "H"+strconv.Itoa(writingRow), d.PayMoney/100)
//			//结算金额
//			xlsNew.SetCellValue(templateSheetName, "I"+strconv.Itoa(writingRow), d.SettlementMoney/100)
//			//服务状态
//			xlsNew.SetCellValue(templateSheetName, "J"+strconv.Itoa(writingRow), d.TradeStateDesc)
//
//			//借出时间
//			xlsNew.SetCellValue(templateSheetName, "K"+strconv.Itoa(writingRow), time.Unix(int64(d.CreateAt), 0).Format("2006-01-02 15:04"))
//			//归还时间
//			xlsNew.SetCellValue(templateSheetName, "L"+strconv.Itoa(writingRow), time.Unix(int64(d.EndAt), 0).Format("2006-01-02 15:04"))
//
//			writingRow++
//
//		}
//	}
//	//设置宽度
//	xlsNew.SetColWidth(templateSheetName, "A", "B", 40)
//	xlsNew.SetColWidth(templateSheetName, "F", "G", 30)
//	xlsNew.SetColWidth(templateSheetName, "J", "L", 30)
//	xlsNew.SetColWidth(templateSheetName, "D", "D", 20)
//	//设置默认打开的表单
//	xlsNew.SetActiveSheet(excelSheet)
//
//	// 退出保存文件，退出
//	errSave := xlsNew.SaveAs(exportFile)
//	if errSave != nil {
//		log.Println("TPFuncDataWriteTplExcel save data to excel  error:", errSave.Error())
//		return true, errSave
//
//	} else {
//		return true, nil
//	}
//}

func OrderDataWriteTplExcel(data []*orders.OrderList, templateSheetName, templateFile, newFileName string) (bool, error) {
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		return false, err
	}

	file := xlsx.NewFile()
	//设置字体
	xlsx.SetDefaultFont(11, "宋体")
	sheet, err := file.AddSheet(templateSheetName)
	if err != nil {
		return false, err
	}

	titleRow := sheet.AddRow()
	var rowsTitle []string
	rows := template.GetRows(templateSheetName)
	rowsTitle = rows[0][0 : len(rows[0])-2]
	var xlsRows []interface{}
	for _, v := range rowsTitle { xlsRows = append(xlsRows, v) }
	xlsRow := NewRow(titleRow, xlsRows)
	err = xlsRow.SetRowTitle()
	if err != nil { return false, err }

	for _, v := range data {
		currentRow := sheet.AddRow()

		//tmp := make([]string, 0)
		var addCellValue []interface{}

		addCellValue = append(addCellValue, v.TradeNo)
		addCellValue = append(addCellValue, v.OutTradeNo)

		var statusText = ""
		//订单状态
		switch v.State {
		case 1:
			statusText = "等待下单"
		case 2:
			statusText = "使用中"
		case 3:
			statusText = "已撤销"
		case 4:
			statusText = "交易失败"
		case 5:
			statusText = "已完成"
		case 6:
			statusText = "支付超时"
		case 7:
			statusText = "待守约"
		case 10:
			statusText = "已删除"
		}

		addCellValue = append(addCellValue, v.MemberName)
		addCellValue = append(addCellValue, statusText)

		//订单来源
		var orderFromStr = ""
		if v.OrderFrom == "weixin" {
			orderFromStr = "微信"
		} else {
			orderFromStr = "支付宝"
		}

		payMoney, _ := decimal.NewFromInt(v.PayMoney).Div(decimal.NewFromInt(100)).Float64()
		settlementMoney, _ := decimal.NewFromInt(v.SettlementMoney).Div(decimal.NewFromInt(100)).Float64()
		couponMoney, _ := decimal.NewFromInt(v.CouponMoney).Div(decimal.NewFromInt(100)).Float64()

		addCellValue = append(addCellValue, orderFromStr)
		addCellValue = append(addCellValue, v.HotelId)
		addCellValue = append(addCellValue, v.PlaceId)
		addCellValue = append(addCellValue, v.PlaceName)
		addCellValue = append(addCellValue, v.DeviceSn)
		addCellValue = append(addCellValue, payMoney)
		addCellValue = append(addCellValue, settlementMoney)
		addCellValue = append(addCellValue, v.TradeStateDesc)
		var IsCoupon = ""
		if v.CouponType == 1 {
			IsCoupon = "是"
		} else {
			IsCoupon = "否"
		}
		addCellValue = append(addCellValue, IsCoupon)
		addCellValue = append(addCellValue, couponMoney)
		addCellValue = append(addCellValue, time.Unix(int64(v.CreateAt), 0).Format("2006-01-02 15:04"))
		addCellValue = append(addCellValue, time.Unix(int64(v.EndAt), 0).Format("2006-01-02 15:04"))

		xlsRow := NewRow(currentRow, addCellValue)
		err := xlsRow.GenerateRow()
		if err != nil {
			return false, err
		}
	}

	//设置宽度
	sheet.Cols[0].Width = 28
	sheet.Cols[1].Width = 30
	sheet.Cols[7].Width = 30
	sheet.Cols[8].Width = 16
	sheet.Cols[11].Width = 20
	sheet.Cols[14].Width = 20
	sheet.Cols[15].Width = 20

	err = file.Save(newFileName)
	if err != nil {
		return false, err
	}
	return true, nil
}

func OrderDataWriteTplExcelToKf(data []*orders.OrderList, templateSheetName, templateFile, newFileName string) (bool, error) {
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		return false, err
	}

	file := xlsx.NewFile()
	//设置字体
	xlsx.SetDefaultFont(11, "宋体")
	sheet, err := file.AddSheet(templateSheetName)
	if err != nil {
		return false, err
	}

	titleRow := sheet.AddRow()
	var rowsTitle []string
	rows := template.GetRows(templateSheetName)
	rowsTitle = rows[0][0 : len(rows[0])-2]
	var xlsRows []interface{}
	for _, v := range rowsTitle { xlsRows = append(xlsRows, v) }
	xlsRow := NewRow(titleRow, xlsRows)
	err = xlsRow.SetRowTitle()
	if err != nil { return false, err }

	for _, v := range data {
		currentRow := sheet.AddRow()

		//tmp := make([]string, 0)
		var addCellValue []interface{}

		addCellValue = append(addCellValue, v.TradeNo)
		addCellValue = append(addCellValue, v.OutTradeNo)

		var statusText = ""
		//订单状态
		switch v.State {
		case 1:
			statusText = "等待下单"
		case 2:
			statusText = "使用中"
		case 3:
			statusText = "已撤销"
		case 4:
			statusText = "交易失败"
		case 5:
			statusText = "已完成"
		case 6:
			statusText = "支付超时"
		case 7:
			statusText = "待守约"
		case 10:
			statusText = "已删除"
		}

		addCellValue = append(addCellValue, v.MemberName)
		addCellValue = append(addCellValue, statusText)

		//订单来源
		var orderFromStr = ""
		if v.OrderFrom == "weixin" {
			orderFromStr = "微信"
		} else {
			orderFromStr = "支付宝"
		}

		payMoney, _ := decimal.NewFromInt(v.PayMoney).Div(decimal.NewFromInt(100)).Float64()
		settlementMoney, _ := decimal.NewFromInt(v.SettlementMoney).Div(decimal.NewFromInt(100)).Float64()
		couponMoney, _ := decimal.NewFromInt(v.CouponMoney).Div(decimal.NewFromInt(100)).Float64()

		addCellValue = append(addCellValue, orderFromStr)
		addCellValue = append(addCellValue, v.HotelId)
		addCellValue = append(addCellValue, v.PlaceId)
		addCellValue = append(addCellValue, v.PlaceName)
		addCellValue = append(addCellValue, v.DeviceSn)
		addCellValue = append(addCellValue, payMoney)
		addCellValue = append(addCellValue, settlementMoney)
		addCellValue = append(addCellValue, v.TradeStateDesc)
		var IsCoupon = ""
		if v.CouponType == 1 {
			IsCoupon = "是"
		} else {
			IsCoupon = "否"
		}
		addCellValue = append(addCellValue, IsCoupon)
		addCellValue = append(addCellValue, couponMoney)
		addCellValue = append(addCellValue, time.Unix(int64(v.CreateAt), 0).Format("2006-01-02 15:04"))
		addCellValue = append(addCellValue, time.Unix(int64(v.EndAt), 0).Format("2006-01-02 15:04"))

		var IsDrawText = ""

		if v.IsDraw == 1 {
			IsDrawText = "是"
		} else {

			IsDrawText = "否"
		}

		addCellValue = append(addCellValue, IsDrawText)

		xlsRow := NewRow(currentRow, addCellValue)
		err := xlsRow.GenerateRow()
		if err != nil {
			return false, err
		}
	}

	//设置宽度
	sheet.Cols[0].Width = 28
	sheet.Cols[1].Width = 30
	sheet.Cols[7].Width = 20
	sheet.Cols[8].Width = 20
	sheet.Cols[11].Width = 20
	sheet.Cols[14].Width = 20
	sheet.Cols[15].Width = 20

	err = file.Save(newFileName)
	if err != nil {
		return false, err
	}
	return true, nil
}

func WritingRowData(writingRow int, templateSheetName, exportFile string, data []*orders.OrderExportModel, index string) {
	fmt.Println(writingRow, exportFile, time.Now())
	// 打开复制文件
	xlsNew, errOpenNew := excelize.OpenFile(exportFile)
	if errOpenNew != nil {
		logs.Error("TPFuncDataWriteTplExcel write data to excel open exportFile error:%v", errOpenNew.Error())
	}
	if index == "2" {
		for _, d := range data {
			//设置行高

			xlsNew.SetRowHeight(templateSheetName, writingRow, 20)
			//订单编号
			xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.TradeNo)

			//第三方订单编号
			xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.OutTradeNo)
			var statusText = ""
			//订单状态
			switch d.State {
			case 1:
				statusText = "等待下单"
			case 2:
				statusText = "使用中"
			case 3:
				statusText = "已撤销"
			case 4:
				statusText = "交易失败"
			case 5:
				statusText = "已完成"
			case 6:
				statusText = "支付超时"
			case 7:
				statusText = "待守约"
			case 10:
				statusText = "已删除"

			}

			xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), statusText)
			//会员名称
			xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.MemberName)

			//订单来源
			var orderFromStr = ""
			if d.OrderFrom == "weixin" {
				orderFromStr = "微信"
			} else {
				orderFromStr = "支付宝"
			}

			xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), orderFromStr)

			//商户名称
			xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), d.PlaceName)

			//设备标识
			xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), d.DeviceSn)
			//支付金额
			xlsNew.SetCellValue(templateSheetName, "H"+strconv.Itoa(writingRow), d.PayMoney/100)
			//结算金额
			xlsNew.SetCellValue(templateSheetName, "I"+strconv.Itoa(writingRow), d.SettlementMoney/100)
			//服务状态
			xlsNew.SetCellValue(templateSheetName, "J"+strconv.Itoa(writingRow), d.TradeStateDesc)

			//借出时间
			xlsNew.SetCellValue(templateSheetName, "K"+strconv.Itoa(writingRow), time.Unix(int64(d.CreateAt), 0).Format("2006-01-02 15:04"))
			//归还时间
			xlsNew.SetCellValue(templateSheetName, "L"+strconv.Itoa(writingRow), time.Unix(int64(d.EndAt), 0).Format("2006-01-02 15:04"))

			var IsDrawText = ""
			if d.IsDraw == 1 {
				IsDrawText = "是"
			} else {
				IsDrawText = "否"
			}

			//是否奖励
			xlsNew.SetCellValue(templateSheetName, "M"+strconv.Itoa(writingRow), IsDrawText)

			writingRow++

		}
	} else {
		for _, d := range data {
			//设置行高

			xlsNew.SetRowHeight(templateSheetName, writingRow, 20)
			//订单编号
			xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.TradeNo)

			//第三方订单编号
			xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.OutTradeNo)
			var statusText = ""
			//订单状态
			switch d.State {
			case 1:
				statusText = "等待下单"
			case 2:
				statusText = "使用中"
			case 3:
				statusText = "已撤销"
			case 4:
				statusText = "交易失败"
			case 5:
				statusText = "已完成"
			case 6:
				statusText = "支付超时"
			case 7:
				statusText = "待守约"
			case 10:
				statusText = "已删除"

			}

			xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), statusText)
			//会员名称
			xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.MemberName)

			//订单来源
			var orderFromStr = ""
			if d.OrderFrom == "weixin" {
				orderFromStr = "微信"
			} else {
				orderFromStr = "支付宝"
			}

			xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), orderFromStr)

			//商户名称
			xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), d.PlaceName)

			//设备标识
			xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), d.DeviceSn)
			//支付金额
			xlsNew.SetCellValue(templateSheetName, "H"+strconv.Itoa(writingRow), d.PayMoney/100)
			//结算金额
			xlsNew.SetCellValue(templateSheetName, "I"+strconv.Itoa(writingRow), d.SettlementMoney/100)
			//服务状态
			xlsNew.SetCellValue(templateSheetName, "J"+strconv.Itoa(writingRow), d.TradeStateDesc)

			//借出时间
			xlsNew.SetCellValue(templateSheetName, "K"+strconv.Itoa(writingRow), time.Unix(int64(d.CreateAt), 0).Format("2006-01-02 15:04"))
			//归还时间
			xlsNew.SetCellValue(templateSheetName, "L"+strconv.Itoa(writingRow), time.Unix(int64(d.EndAt), 0).Format("2006-01-02 15:04"))

			writingRow++

		}
	}

	errSave := xlsNew.Save()
	if errSave != nil {
		logs.Error("TPFuncDataWriteTplExcel save data to excel  error:%v", errSave.Error())
	}
}

//网点导出
func PlacesDataWriteTplExcel(data []*placeresp.PlaceList, templateSheetName, templateFile, newFileName string, isAdmin int) (bool, error) {
	exportFile := newFileName
	xlsNew := excelize.NewFile()
	excelSheet := xlsNew.NewSheet(templateSheetName)

	//获取模板数据
	axis := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	template, err := excelize.OpenFile(templateFile)
	if err != nil { log.Fatal(err) }
	rows := template.GetRows(templateSheetName)
	var indexAxis int
	for i, v := range rows[0] {
		if isAdmin == helper.TypeNumIsAdminOperator || isAdmin == helper.TypeNumIsAdminAdministrator || isAdmin == helper.TypeNumIsAdminStaff {
			xlsNew.SetCellValue(templateSheetName, axis[i]+strconv.Itoa(1), v)
		} else {
			if v == "关联分公司" {
				indexAxis += 1
				continue
			}
			xlsNew.SetCellValue(templateSheetName, axis[i-indexAxis]+strconv.Itoa(1), rows[0][i])

		}

	}

	//第3行开始
	var writingRow = 2

	var BeijingLocation = time.FixedZone("Asia/Shanghai", 8*60*60)
	//转换签收时间戳
	layout := "2006-01-02T15:04:05+08:00"
	if isAdmin == helper.TypeNumIsAdminOperator || isAdmin == helper.TypeNumIsAdminAdministrator || isAdmin == helper.TypeNumIsAdminStaff {
		for _, d := range data {
			//设置行高
			xlsNew.SetRowHeight(templateSheetName, writingRow, 20)
			// 门店ID
			xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.Id)
			// 门店等级
			xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.Level + "(" + decimal.NewFromFloat32(d.UsedRate).StringFixed(2)+ "%)")
			// 网点名称
			xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), d.PlaceName)
			//所属人
			xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.Name)
			//手机号码
			xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), d.Phone)
			//设备
			xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), d.DeviceCount)
			//分红比例
			xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), d.ProfitProportion)
			//上月流水
			xlsNew.SetCellValue(templateSheetName, "H"+strconv.Itoa(writingRow), decimal.NewFromInt(d.LastMonthSettlement).Div(decimal.NewFromInt(100)).StringFixed(2))
			//本月流水
			xlsNew.SetCellValue(templateSheetName, "I"+strconv.Itoa(writingRow), decimal.NewFromInt(d.NowMonthSettlement).Div(decimal.NewFromInt(100)).StringFixed(2))
			// 酒店ID
			xlsNew.SetCellValue(templateSheetName, "J"+strconv.Itoa(writingRow), d.HotelId)
			//上级代理商
			if d.ParentName == "" && d.ParentUserName == "" {
				xlsNew.SetCellValue(templateSheetName, "K"+strconv.Itoa(writingRow), "")
			} else {
				xlsNew.SetCellValue(templateSheetName, "K"+strconv.Itoa(writingRow), d.ParentName + "/" + d.ParentUserName)
			}
			//所属分公司
			if d.BranchName == "" && d.BranchUserName == "" {
				xlsNew.SetCellValue(templateSheetName, "L"+strconv.Itoa(writingRow), "")
			} else {
				xlsNew.SetCellValue(templateSheetName, "L"+strconv.Itoa(writingRow), d.BranchName + "/" + d.BranchUserName)
			}

			//省
			xlsNew.SetCellValue(templateSheetName, "M"+strconv.Itoa(writingRow), d.Province)
			//市
			xlsNew.SetCellValue(templateSheetName, "N"+strconv.Itoa(writingRow), d.City)
			// 创建时间
			xlsNew.SetCellValue(templateSheetName, "O"+strconv.Itoa(writingRow), time.Unix(int64(d.CreateAt), 0).Format("2006-01-02 15:04"))
			// 物流签收时间
			if d.ReceiveTime == "" {
				xlsNew.SetCellValue(templateSheetName, "P"+strconv.Itoa(writingRow), d.ReceiveTime)
			} else {
				receiveTime, _ := time.ParseInLocation(layout, d.ReceiveTime, BeijingLocation)
				//successTime.AddDate(0, 0, -1)
				//receiveUnix := wechatpay.StrTimeToIntByString("2006-01-02", receiveTime.Format("2006-01-02 15:04:05"))
				xlsNew.SetCellValue(templateSheetName, "P"+strconv.Itoa(writingRow), receiveTime.Format("2006-01-02 15:04:05"))
			}
			xlsNew.SetCellValue(templateSheetName, "Q"+strconv.Itoa(writingRow), d.PlaceDesc)
			if d.IsSteward == 1 {
				xlsNew.SetCellValue(templateSheetName, "R"+strconv.Itoa(writingRow), "是")
			} else {
				xlsNew.SetCellValue(templateSheetName, "R"+strconv.Itoa(writingRow), "否")
			}
			if d.IsAd == 1 {
				xlsNew.SetCellValue(templateSheetName, "S"+strconv.Itoa(writingRow), "是")
			} else {
				xlsNew.SetCellValue(templateSheetName, "S"+strconv.Itoa(writingRow), "否")
			}
			writingRow++
		}

		//设置宽度
		xlsNew.SetColWidth(templateSheetName, "C", "C", 30)
		xlsNew.SetColWidth(templateSheetName, "K", "K", 25)
		xlsNew.SetColWidth(templateSheetName, "L", "L", 25)
		xlsNew.SetColWidth(templateSheetName, "O", "O", 16)
		xlsNew.SetColWidth(templateSheetName, "P", "P", 16)

	} else {
		for _, d := range data {
			//设置行高
			xlsNew.SetRowHeight(templateSheetName, writingRow, 20)
			//网点id
			xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.Id)
			// 网点等级
			xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.Level + "(" + decimal.NewFromFloat32(d.UsedRate).StringFixed(2) + "%)")
			// 网点名称
			xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), d.PlaceName)
			//所属人
			xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.Name)
			//手机号码
			xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), d.Phone)
			//设备
			xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), d.DeviceCount)
			//分红比例
			xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), d.ProfitProportion)
			//上月流水
			xlsNew.SetCellValue(templateSheetName, "H"+strconv.Itoa(writingRow), decimal.NewFromInt(d.LastMonthSettlement).Div(decimal.NewFromInt(100)).StringFixed(2))
			//本月流水
			xlsNew.SetCellValue(templateSheetName, "I"+strconv.Itoa(writingRow), decimal.NewFromInt(d.NowMonthSettlement).Div(decimal.NewFromInt(100)).StringFixed(2))
			// 酒店ID
			xlsNew.SetCellValue(templateSheetName, "J"+strconv.Itoa(writingRow), d.HotelId)
			//上级代理商
			if d.ParentName == "" && d.ParentUserName == "" {
				xlsNew.SetCellValue(templateSheetName, "K"+strconv.Itoa(writingRow), "")
			} else {
				xlsNew.SetCellValue(templateSheetName, "K"+strconv.Itoa(writingRow), d.ParentName + "/" + d.ParentUserName)
			}
			//省
			xlsNew.SetCellValue(templateSheetName, "L"+strconv.Itoa(writingRow), d.Province)
			//市
			xlsNew.SetCellValue(templateSheetName, "M"+strconv.Itoa(writingRow), d.City)
			// 创建时间
			xlsNew.SetCellValue(templateSheetName, "N"+strconv.Itoa(writingRow), time.Unix(int64(d.CreateAt), 0).Format("2006-01-02 15:04"))
			// 物流签收时间
			if d.ReceiveTime == "" {
				xlsNew.SetCellValue(templateSheetName, "O"+strconv.Itoa(writingRow), d.ReceiveTime)
			} else {
				receiveTime, _ := time.ParseInLocation(layout, d.ReceiveTime, BeijingLocation)
				//successTime.AddDate(0, 0, -1)
				//receiveUnix := wechatpay.StrTimeToIntByString("2006-01-02", receiveTime.Format("2006-01-02 15:04:05"))
				xlsNew.SetCellValue(templateSheetName, "O"+strconv.Itoa(writingRow), receiveTime.Format("2006-01-02 15:04:05"))
			}
			writingRow++
		}

		//设置宽度
		xlsNew.SetColWidth(templateSheetName, "C", "C", 30)
		xlsNew.SetColWidth(templateSheetName, "K", "K", 25)
		xlsNew.SetColWidth(templateSheetName, "N", "N", 16)
		xlsNew.SetColWidth(templateSheetName, "O", "O", 16)
	}

	//设置默认打开的表单
	xlsNew.SetActiveSheet(excelSheet)

	// 退出保存文件，退出
	errSave := xlsNew.SaveAs(exportFile)
	if errSave != nil {
		log.Println("TPFuncDataWriteTplExcel save data to excel  error:", errSave.Error())
		return true, errSave

	} else {
		return true, nil
	}
}


//门店操作日志导出
func PlacesOperateLogWriteTplExcel(data []placemodel.PlaceOperateLog, templateSheetName, templateFile, newFileName string) (bool, error) {
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		return false, err
	}

	file := xlsx.NewFile()
	//设置字体
	xlsx.SetDefaultFont(11, "宋体")
	sheet, err := file.AddSheet(templateSheetName)
	if err != nil {
		return false, err
	}

	titleRow := sheet.AddRow()
	var rowsTitle []string
	rows := template.GetRows(templateSheetName)
	rowsTitle = rows[0][0 : len(rows[0])-2]
	var xlsRows []interface{}
	for _, v := range rowsTitle { xlsRows = append(xlsRows, v) }
	xlsRow := NewRow(titleRow, xlsRows)
	err = xlsRow.SetRowTitle()
	if err != nil { return false, err }

	var beforField, afterField placeresp.PlaceChangeField
	for _, v := range data {
		currentRow := sheet.AddRow()
		var addCellValue []interface{}
		addCellValue = append(addCellValue, v.OperationUserId)
		addCellValue = append(addCellValue, v.OperationUserName)
		_ = json.Unmarshal([]byte(v.BeforeField), &beforField)
		_ = json.Unmarshal([]byte(v.AfterField), &afterField)
		addCellValue = append(addCellValue, v.PlaceId)
		addCellValue = append(addCellValue, beforField.PlaceName)
		addCellValue = append(addCellValue, afterField.PlaceName)
		addCellValue = append(addCellValue, afterField.Devices)
		addCellValue = append(addCellValue, v.Description)
		addCellValue = append(addCellValue, v.PlaceDesc)
		addCellValue = append(addCellValue, v.PlaceDescAfter)
		addCellValue = append(addCellValue, time.Unix(int64(v.CreateAt), 0).Format("2006-01-02 15:04"))
		xlsRow := NewRow(currentRow, addCellValue)
		err := xlsRow.GenerateRow()
		if err != nil {
			return false, err
		}
	}

	//设置宽度
	//sheet.Cols[0].Width = 28
	//sheet.Cols[1].Width = 30
	//sheet.Cols[2].Width = 20
	//sheet.Cols[3].Width = 15

	err = file.Save(newFileName)
	if err != nil {
		return false, err
	}
	return true, nil
}

func SmallPlacePloyDataWriteTplExcel(data []*placeresp.PlaceListByAll, templateSheetName, templateFile, newFileName string) (bool, error) {
	exportFile := newFileName

	xlsNew := excelize.NewFile()

	excelSheet := xlsNew.NewSheet(templateSheetName)

	//获取模板数据
	fmt.Println(templateFile)
	axis := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		log.Fatal(err)
	}
	rows := template.GetRows(templateSheetName)
	var indexAxis int
	for i, _ := range rows[0] {
		xlsNew.SetCellValue(templateSheetName, axis[i-indexAxis]+strconv.Itoa(1), rows[0][i])
	}

	//第3行开始
	var writingRow = 2
	for _, d := range data {
		xlsNew.SetRowHeight(templateSheetName, writingRow, 20)
		//网点id
		xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.Id)
		// 网点等级
		xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.Level)
		// 网点名称
		xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), d.PlaceName)
		//所属人
		xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.Name)
		//手机号码
		xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), d.Phone)
		//设备
		xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), d.DeviceCount)

		xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), decimal.NewFromInt(d.LastMonthSettlement).Div(decimal.NewFromInt(100)).StringFixed(2))
		//本月流水
		xlsNew.SetCellValue(templateSheetName, "H"+strconv.Itoa(writingRow), decimal.NewFromInt(d.NowMonthSettlement).Div(decimal.NewFromInt(100)).StringFixed(2))

		//省
		xlsNew.SetCellValue(templateSheetName, "I"+strconv.Itoa(writingRow), d.Province)
		//市
		xlsNew.SetCellValue(templateSheetName, "J"+strconv.Itoa(writingRow), d.City)
		xlsNew.SetCellValue(templateSheetName, "K"+strconv.Itoa(writingRow), d.DrawStr)
		xlsNew.SetCellValue(templateSheetName, "L"+strconv.Itoa(writingRow), len(strings.Split(d.DrawStr, ",")))
		// 创建时间
		xlsNew.SetCellValue(templateSheetName, "M"+strconv.Itoa(writingRow), time.Unix(int64(d.CreateAt), 0).Format("2006-01-02 15:04"))
		writingRow++
	}

	//设置宽度
	//xlsNew.SetColWidth(templateSheetName, "A", "B", 20)
	xlsNew.SetColWidth(templateSheetName, "C", "C", 30)
	xlsNew.SetColWidth(templateSheetName, "E", "E", 15)
	xlsNew.SetColWidth(templateSheetName, "K", "I", 30)
	xlsNew.SetColWidth(templateSheetName, "O", "O", 20)
	xlsNew.SetColWidth(templateSheetName, "P", "P", 20)

	//设置默认打开的表单
	xlsNew.SetActiveSheet(excelSheet)

	// 退出保存文件，退出
	errSave := xlsNew.SaveAs(exportFile)
	if errSave != nil {
		log.Println("TPFuncDataWriteTplExcel save data to excel  error:", errSave.Error())
		return true, errSave

	} else {
		return true, nil
	}
}


func PlacePloyDataWriteTplExcel(data []*placeresp.PlaceListByAll, templateSheetName, templateFile, newFileName string) (bool, error) {
	exportFile := newFileName

	xlsNew := excelize.NewFile()

	excelSheet := xlsNew.NewSheet(templateSheetName)

	//获取模板数据
	fmt.Println(templateFile)
	axis := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		log.Fatal(err)
	}
	rows := template.GetRows(templateSheetName)
	var indexAxis int
	for i, _ := range rows[0] {
		xlsNew.SetCellValue(templateSheetName, axis[i-indexAxis]+strconv.Itoa(1), rows[0][i])
	}

	//第3行开始
	var writingRow = 2
	for _, d := range data {
		xlsNew.SetRowHeight(templateSheetName, writingRow, 20)
		//网点id
		xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.Id)
		// 网点等级
		xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.Level)
		// 网点名称
		xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), d.PlaceName)
		//所属人
		xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.Name)
		//手机号码
		xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), d.Phone)
		//设备
		xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), d.DeviceCount)

		xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), decimal.NewFromInt(d.LastMonthSettlement).Div(decimal.NewFromInt(100)).StringFixed(2))
		//本月流水
		xlsNew.SetCellValue(templateSheetName, "H"+strconv.Itoa(writingRow), decimal.NewFromInt(d.NowMonthSettlement).Div(decimal.NewFromInt(100)).StringFixed(2))

		//省
		xlsNew.SetCellValue(templateSheetName, "I"+strconv.Itoa(writingRow), d.Province)
		//市
		xlsNew.SetCellValue(templateSheetName, "J"+strconv.Itoa(writingRow), d.City)
		xlsNew.SetCellValue(templateSheetName, "K"+strconv.Itoa(writingRow), d.Range)
		xlsNew.SetCellValue(templateSheetName, "L"+strconv.Itoa(writingRow), d.Num)
		// 创建时间
		xlsNew.SetCellValue(templateSheetName, "M"+strconv.Itoa(writingRow), time.Unix(int64(d.CreateAt), 0).Format("2006-01-02 15:04"))
		writingRow++
	}

	//设置宽度
	//xlsNew.SetColWidth(templateSheetName, "A", "B", 20)
	xlsNew.SetColWidth(templateSheetName, "C", "C", 30)
	xlsNew.SetColWidth(templateSheetName, "E", "E", 15)
	xlsNew.SetColWidth(templateSheetName, "K", "I", 30)
	xlsNew.SetColWidth(templateSheetName, "O", "O", 20)
	xlsNew.SetColWidth(templateSheetName, "P", "P", 20)

	//设置默认打开的表单
	xlsNew.SetActiveSheet(excelSheet)

	// 退出保存文件，退出
	errSave := xlsNew.SaveAs(exportFile)
	if errSave != nil {
		log.Println("TPFuncDataWriteTplExcel save data to excel  error:", errSave.Error())
		return true, errSave

	} else {
		return true, nil
	}
}

//设备导出
func DeviceDataWriteTplExcel(data []*devices.DeviceType, templateSheetName, templateFile, newFileName string) (bool, error) {
	exportFile := newFileName
	xlsNew := excelize.NewFile()
	excelSheet := xlsNew.NewSheet(templateSheetName)

	//获取模板数据
	axis := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	template, err := excelize.OpenFile(templateFile)
	if err != nil { log.Fatal("", err) }
	rows := template.GetRows(templateSheetName)
	for i, v := range rows[0] {
		xlsNew.SetCellValue(templateSheetName, axis[i]+strconv.Itoa(1), v)
	}
	//第3行开始
	var writingRow = 2
	for _, d := range data {
		//设置行高
		xlsNew.SetRowHeight(templateSheetName, writingRow, 20)
		xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.DeviceSn) // 设备标识
		xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.DomainUrl + d.DeviceSn) // 二维码规则
		xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), d.DeviceTypeName)
		xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.PlaceId)
		xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), d.PlaceName)
		var prodTypeName string
		switch d.ProdType {
		case 1:
			prodTypeName = "密码线充"
		case 2:
			prodTypeName = "放水机"
		case 3:
			prodTypeName = "两口机"
		}
		xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), prodTypeName)
		writingRow++
	}

	//设置宽度
	xlsNew.SetColWidth(templateSheetName, "A", "A", 20)
	xlsNew.SetColWidth(templateSheetName, "B", "B", 60)
	xlsNew.SetColWidth(templateSheetName, "C", "C", 17)

	//设置默认打开的表单
	xlsNew.SetActiveSheet(excelSheet)

	// 退出保存文件，退出
	errSave := xlsNew.SaveAs(exportFile)
	if errSave != nil {
		log.Println("TPFuncDataWriteTplExcel save data to excel  error:", errSave.Error())
		return true, errSave

	} else {
		return true, nil
	}
}

//网点统计导出
func PlaceTotalWriteTplExcel(data []*placeincome.PlaceIncomeExportModel, templateSheetName, templateFile, newFileName string, monthType int) (bool, error) {

	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		log.Fatal(err)
	}
	rows := template.GetRows(templateSheetName)

	file := xlsx.NewFile()
	//设置字体
	xlsx.SetDefaultFont(11, "宋体")
	sheet, err := file.AddSheet(templateSheetName)
	if err != nil {
		return false, err
	}
	//设置标题
	titleRow := sheet.AddRow()
	var xlsRows []interface{}

	for _, v := range rows[0] {
		xlsRows = append(xlsRows, v)
	}

	xlsRow := NewRow(titleRow, xlsRows)
	err = xlsRow.SetRowTitle()
	if err != nil {
		return false, err
	}

	for _, d := range data {
		currentRow := sheet.AddRow()

		var addCellValue []interface{}

		addCellValue = append(addCellValue, time.Unix(int64(d.PlaceCreateAt), 0).Format("2006-01-02 15:04")) //进店日期

		addCellValue = append(addCellValue, d.Province) // 省份
		addCellValue = append(addCellValue, d.City)     // 城市

		addCellValue = append(addCellValue, d.PlaceName) // 酒店名称

		Income, _ := decimal.NewFromInt(d.Income).Div(decimal.NewFromInt(100)).Float64()
		settlementMoney, _ := decimal.NewFromInt(d.SettlementMoney).Div(decimal.NewFromInt(100)).Float64()

		addCellValue = append(addCellValue, d.HotelId)        //酒店ID
		addCellValue = append(addCellValue, d.DeviceCount)    //激活设备量
		addCellValue = append(addCellValue, d.ScanCount)      //扫码量
		addCellValue = append(addCellValue, d.ScanCodeUser)   //扫码用户
		addCellValue = append(addCellValue, d.ActiveDevCount) //有产设备量
		addCellValue = append(addCellValue, d.CompleteCount)  //完成订单量
		addCellValue = append(addCellValue, settlementMoney)  //销售额
		addCellValue = append(addCellValue, Income)           // 收益

		activeStatusText := ""

		if d.LogisticsOrderNo == "上门配送" || d.ActiveStatus == 1 || d.FirstOrderDate != "" {
			activeStatusText = "已激活"
		} else {
			activeStatusText = "未激活"
		}

		//if   d.ActiveStatus == 0 {
		//	activeStatusText = "未激活"
		//}
		//if d.LogisticsOrderNo == "上门配送" || d.FirstOrderDate != "" {
		//	activeStatusText = "已激活"
		//} else {
		//	activeStatusText = "未激活"
		//}
		var BeijingLocation = time.FixedZone("Asia/Shanghai", 8*60*60)
		if monthType == 1 {
			//转换签收时间戳
			layout := "2006-01-02T15:04:05+08:00"
			successTime, _ := time.ParseInLocation(layout, d.ReceiveTime, BeijingLocation)
			//successTime.AddDate(0, 0, -1)
			receiveUnix := wechatpay.StrTimeToIntByString("2006-01-02", successTime.Format("2006-01-02"))
			//转换统计时间戳
			//incomeTime, _ := time.ParseInLocation("2006-01-02", d.IncomeDay, loc)
			incomeUnix := wechatpay.StrTimeToIntByString("2006-01-02", d.IncomeDay)

			//如果签收时间为空，则以首单时间为准
			//首单时间
			firstOrderUnix := wechatpay.StrTimeToIntByString("2006-01-02", d.FirstOrderDate)
			if firstOrderUnix < incomeUnix && d.ReceiveTime == "" {
				activeStatusText = "已激活"
			}
			//如果签收时间大于统计时间，则以首单时间为准
			if receiveUnix > incomeUnix && d.FirstOrderDate != "" {
				activeStatusText = "已激活"
			}
			//如果首单时间大于统计时间且签收时间大于统计时间，则未激活
			if firstOrderUnix > incomeUnix && receiveUnix > incomeUnix && firstOrderUnix > 0 && receiveUnix > 0 {
				activeStatusText = "未激活"
			}
			//如果签收大于统计时间，且首单时间为空，则未激活
			if receiveUnix > incomeUnix && d.ReceiveTime != "" && d.FirstOrderDate == "" {
				activeStatusText = "未激活"
			}
			//如果首单时间大于统计时间，且签收时间为空，则未激活
			if firstOrderUnix > incomeUnix && d.FirstOrderDate != "" && d.ReceiveTime == "" {
				activeStatusText = "未激活"
			}
			//如果两个时间都为空，则未激活
			if d.FirstOrderDate == "" && d.ReceiveTime == "" {
				activeStatusText = "未激活"
			}

			//fmt.Println("DAYI，", d.IncomeDay, successTime.Format("2006-01-02 15:04:05"), d.FirstOrderDate, firstOrderUnix, receiveUnix, incomeUnix)

		}

		if monthType == 2 {
			//转换签收时间戳

			layout := "2006-01-02T15:04:05+08:00"
			successTime, _ := time.Parse(layout, d.ReceiveTime)
			//successTime = successTime.AddDate(0, -1, 0)
			receiveUnix := wechatpay.StrTimeToIntByString("2006-01", successTime.Format("2006-01"))

			//转换统计时间戳
			//incomeTime, _ := time.ParseInLocation("2006-01", d.IncomeDay, loc)
			//incomeUnix := incomeTime.Unix()
			incomeUnix := wechatpay.StrTimeToIntByString("2006-01", d.IncomeDay)

			//首单时间 月统计
			var BeijingLocation = time.FixedZone("Asia/Shanghai", 8*60*60)
			firstOrderTime, _ := time.ParseInLocation("2006-01", d.FirstOrderDate, BeijingLocation)
			//firstOrderTime = firstOrderTime.AddDate(0, -1, 0)
			firstOrderUnix := firstOrderTime.Unix()
			if firstOrderUnix < incomeUnix && d.ReceiveTime == "" {
				activeStatusText = "已激活"
			}
			//如果签收时间大于统计时间，则以首单时间为准
			if receiveUnix > incomeUnix && d.FirstOrderDate != "" {
				activeStatusText = "已激活"
			}
			//如果首单时间大于统计时间且签收时间大于统计时间，则未激活
			if firstOrderUnix > incomeUnix && receiveUnix > incomeUnix && firstOrderUnix > 0 && receiveUnix > 0 {
				activeStatusText = "未激活"
			}
			//如果签收大于统计时间，且首单时间为空，则未激活
			if receiveUnix > incomeUnix && d.ReceiveTime != "" && d.FirstOrderDate == "" {
				activeStatusText = "未激活"
			}
			//如果首单时间大于统计时间，且签收时间为空，则未激活
			if firstOrderUnix > incomeUnix && d.FirstOrderDate != "" && d.ReceiveTime == "" {
				activeStatusText = "未激活"
			}
			//如果两个时间都为空，则未激活
			if d.FirstOrderDate == "" && d.ReceiveTime == "" {
				activeStatusText = "未激活"
			}

		}

		//if receiveUnix < incomeUnix && d.ReceiveTime != "" {
		//	activeStatusText = "已激活"
		//}

		if d.ReturnStatus == 1 {
			activeStatusText = "已退回"
		}

		var receiveTimeStr string
		if d.ReceiveTime != "" {
			layout := "2006-01-02T15:04:05+08:00"
			successTime, _ := time.Parse(layout, d.ReceiveTime)
			//receiveTimeUnix := wechatpay.StrTimeToInt(d.ReceiveTime)
			//receiveTimeStr = time.Unix(receiveTimeUnix, 0).Format("2006-01-02 15:04:05")
			receiveTimeStr = successTime.Format("2006-01-02 15:04:05")
			//loc, _ := time.LoadLocation("Local")
			//receiveTime, _ := time.ParseInLocation("2006-01-02 15:04:05", d.ReceiveTime, loc)
			//receiveTimeStr = receiveTime.Format("2006-01-02 15:04:05")

			//receiveTimeStr = d.ReceiveTime
		} else {
			receiveTimeStr = ""
		}
		addCellValue = append(addCellValue, receiveTimeStr)   //签收时间
		addCellValue = append(addCellValue, activeStatusText) //激活状态
		addCellValue = append(addCellValue, d.ReturnTime)     //撤出时间
		addCellValue = append(addCellValue, d.FirstOrderDate) //首单时间
		addCellValue = append(addCellValue, d.IncomeDay)      //统计时间
		xlsRow := NewRow(currentRow, addCellValue)
		err := xlsRow.GenerateRow()
		if err != nil {
			return false, err
		}

	}
	sheet.Cols[0].Width = 20.5
	sheet.Cols[1].Width = 20.5
	sheet.Cols[3].Width = 20.5
	sheet.Cols[12].Width = 23
	sheet.Cols[14].Width = 23
	sheet.Cols[15].Width = 23
	sheet.Cols[16].Width = 20
	err = file.Save(newFileName)
	if err != nil {
		return false, err
	}
	return true, nil

}

//退款列表导出
func RefundDataWriteTplExcel(data []*refunds.RefundModel, templateSheetName, templateFile, newFileName string) (bool, error) {
	//templateFile := "./static/excel_template/template_order.xls"
	exportFile := newFileName

	xlsNew := excelize.NewFile()

	index := xlsNew.NewSheet(templateSheetName)

	//获取模板数据
	//fmt.Println(templateFile)
	axis := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		log.Fatal(err)
	}
	rows := template.GetRows(templateSheetName)
	for i, v := range rows[0] {
		xlsNew.SetCellValue(templateSheetName, axis[i]+strconv.Itoa(1), v)
	}

	//第3行开始
	var writingRow = 2
	//var writeErr error

	for _, d := range data {
		//设置行高

		xlsNew.SetRowHeight(templateSheetName, writingRow, 20)

		//				创建时间
		//设备标识

		// SUCCESS-退款成功 CHANGE-退款异常 REFUNDCLOSE—退款关闭
		var statusText = ""
		if d.RefundStatus == "SUCCESS" {
			statusText = "退款成功"
		} else if d.RefundStatus == "CHANGE" {
			statusText = "退款成功"
		} else if d.RefundStatus == "REFUNDCLOSE" {
			statusText = "退款关闭"
		}
		//退款状态
		xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), statusText)
		// 订单编号
		xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.TradeNo)
		// 第三方订单号
		xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), d.OutTradeNo)
		//退款订单号
		xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.RefundNo)
		// 第三方退款订单号
		xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), d.OutRefundNo)
		// 退款金额
		xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), decimal.NewFromInt(d.RefundFee).Div(decimal.NewFromInt(100)).StringFixed(2))
		// 订单总金额
		xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), decimal.NewFromInt(d.TotalFee).Div(decimal.NewFromInt(100)).StringFixed(2))
		// 退款入账账户
		xlsNew.SetCellValue(templateSheetName, "H"+strconv.Itoa(writingRow), d.RefundRecvAccout)
		// 退款日期
		xlsNew.SetCellValue(templateSheetName, "I"+strconv.Itoa(writingRow), time.Unix(int64(d.SuccessTime), 0).Format("2006-01-02 15:04"))

		writingRow++
	}

	//设置宽度
	//xlsNew.SetColWidth(templateSheetName, "A", "C", 15)
	//xlsNew.SetColWidth(templateSheetName, "E", "E", 20)

	//设置默认打开的表单
	xlsNew.SetActiveSheet(index)

	// 退出保存文件，退出
	errSave := xlsNew.SaveAs(exportFile)
	if errSave != nil {
		log.Println("TPFuncDataWriteTplExcel save data to excel  error:", errSave.Error())
		return true, errSave

	} else {
		return true, nil
	}
}

//故障列表导出
func RepairDataWriteTplExcel(data []*reports.ReportList, templateSheetName, templateFile, newFileName string) (bool, error) {
	//templateFile := "./static/excel_template/template_order.xls"
	exportFile := newFileName

	xlsNew := excelize.NewFile()

	index := xlsNew.NewSheet(templateSheetName)

	//获取模板数据
	//fmt.Println("templateFile: "+ templateFile)
	axis := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		log.Fatal(err)
	}
	rows := template.GetRows(templateSheetName)
	for i, v := range rows[0] {
		xlsNew.SetCellValue(templateSheetName, axis[i]+strconv.Itoa(1), v)
	}

	//第3行开始
	var writingRow = 2
	//var writeErr error

	for _, d := range data {
		//设置行高

		xlsNew.SetRowHeight(templateSheetName, writingRow, 20)

		//				创建时间
		//设备标识
		var statusText = ""
		if d.Status == 1 {
			statusText = "等待处理"
		} else if d.Status == 2 {
			statusText = "已处理"
		}

		var typeText = ""
		if d.Type == 1 {
			typeText = "无法充电"
		} else if d.Status == 2 {
			typeText = "无法连接"
		} else if d.Status == 3 {
			typeText = "设备故障"
		} else if d.Status == 4 {
			typeText = "其他"
		}

		// 订单编号
		xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.TradeNo)
		// 申报时间
		xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), time.Unix(int64(d.CreateAt), 0).Format("2006-01-02 15:04"))
		// 申报设备
		xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), d.DeviceSn)
		//申报人
		xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), "")
		// 联系电话
		xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), d.Phone)
		// 申报网点
		xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), d.PlaceName)
		// 故障类型
		xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), typeText)
		// 故障描述
		xlsNew.SetCellValue(templateSheetName, "H"+strconv.Itoa(writingRow), d.Desc)
		// 故障状态
		xlsNew.SetCellValue(templateSheetName, "I"+strconv.Itoa(writingRow), statusText)
		xlsNew.SetCellValue(templateSheetName, "J"+strconv.Itoa(writingRow), d.ReportDesc)
		var prodTypeName string
		switch d.ProdType {
		case 1:
			prodTypeName = "密码线充"
		case 2:
			prodTypeName = "放水机"
		case 3:
			prodTypeName = "两口机"
		}
		xlsNew.SetCellValue(templateSheetName, "K"+strconv.Itoa(writingRow), prodTypeName)
		xlsNew.SetCellValue(templateSheetName, "L"+strconv.Itoa(writingRow), d.Room)
		writingRow++
	}

	//设置默认打开的表单
	xlsNew.SetActiveSheet(index)

	// 退出保存文件，退出
	errSave := xlsNew.SaveAs(exportFile)
	if errSave != nil {
		log.Println("TPFuncDataWriteTplExcel save data to excel  error:", errSave.Error())
		return true, errSave

	} else {
		return true, nil
	}
}

//代理提现
func AgentCashWriteTplExcel(data []*cashlog.UserWithdrawalLog, templateSheetName, templateFile, newFileName string) (bool, error) {
	//templateFile := "./static/excel_template/template_order.xls"
	exportFile := newFileName

	xlsNew := excelize.NewFile()

	index := xlsNew.NewSheet(templateSheetName)

	//获取模板数据
	//fmt.Println(templateFile)
	axis := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		log.Fatal(err)
	}
	rows := template.GetRows(templateSheetName)
	for i, v := range rows[0] {
		xlsNew.SetCellValue(templateSheetName, axis[i]+strconv.Itoa(1), v)
		xlsNew.SetRowHeight(templateSheetName, 1, 20)
	}

	//第3行开始
	var writingRow = 2
	//var writeErr error

	for _, d := range data {
		//设置行高

		xlsNew.SetRowHeight(templateSheetName, writingRow, 15)

		status := ""
		switch d.State {
		case 1:
			status = "未处理"
		case 2:
			status = "已处理"
		case 3:
			status = "处理失败"
		case 4:
			status = "处理中"
		}

		//申请编号
		xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.ID)
		// 订单编号
		xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.TradeNo)
		// 申请人
		xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), d.Name)
		// 手机号码
		xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.Phone)
		// 提现描述
		xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), d.Desc)
		// 退款金额
		xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), fmt.Sprintf("%.2f", float32(d.Money)/100)+"元")
		// 状态
		xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), status)
		// 创建时间
		xlsNew.SetCellValue(templateSheetName, "H"+strconv.Itoa(writingRow), time.Unix(int64(d.CreateAt), 0).Format("2006-01-02 15:04"))
		// 累计提现
		xlsNew.SetCellValue(templateSheetName, "I"+strconv.Itoa(writingRow), d.CashMoney)
		// 可提现
		xlsNew.SetCellValue(templateSheetName, "J"+strconv.Itoa(writingRow), d.Balance)

		writingRow++
	}

	//设置宽度
	xlsNew.SetColWidth(templateSheetName, "B", "B", 40)
	xlsNew.SetColWidth(templateSheetName, "C", "C", 25)
	xlsNew.SetColWidth(templateSheetName, "D", "D", 17)
	xlsNew.SetColWidth(templateSheetName, "H", "H", 20)
	//xlsNew.SetColWidth(templateSheetName, "E", "E", 20)

	//设置默认打开的表单
	xlsNew.SetActiveSheet(index)

	// 退出保存文件，退出
	errSave := xlsNew.SaveAs(exportFile)
	if errSave != nil {
		log.Println("TPFuncDataWriteTplExcel save data to excel  error:", errSave.Error())
		return true, errSave

	} else {
		return true, nil
	}
}

// 分公司用户
func BranchUserListWriteTplExcel(data []*users.UserInfoWithKA, templateSheetName, templateFile, newFileName string) (bool, error) {
	//templateFile := "./static/excel_template/template_order.xls"
	exportFile := newFileName

	xlsNew := excelize.NewFile()

	index := xlsNew.NewSheet(templateSheetName)

	//获取模板数据
	//fmt.Println(templateFile)
	axis := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		log.Fatal(err)
	}
	rows := template.GetRows(templateSheetName)
	for i, v := range rows[0] {
		xlsNew.SetCellValue(templateSheetName, axis[i]+strconv.Itoa(1), v)
		xlsNew.SetRowHeight(templateSheetName, 1, 20)
	}

	//第3行开始
	var writingRow = 2
	//var writeErr error

	for _, d := range data {
		//设置行高

		xlsNew.SetRowHeight(templateSheetName, writingRow, 15)

		// ID
		xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.ID)
		// 名称
		xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), "【"+d.RoleName+"】"+d.UserName)
		// 用户编号
		xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), d.UserNo)
		// 联系人姓名
		xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.Name)
		// 手机号码
		xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), d.Phone)
		// 加入时间
		xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), time.Unix(int64(d.CreateAt), 0).Format("2006-01-02 15:04"))

		writingRow++
	}

	//设置宽度
	xlsNew.SetColWidth(templateSheetName, "B", "B", 30)
	xlsNew.SetColWidth(templateSheetName, "C", "C", 26)
	xlsNew.SetColWidth(templateSheetName, "E", "E", 15)
	xlsNew.SetColWidth(templateSheetName, "F", "F", 20)
	//xlsNew.SetColWidth(templateSheetName, "E", "E", 20)

	//设置默认打开的表单
	xlsNew.SetActiveSheet(index)

	// 退出保存文件，退出
	errSave := xlsNew.SaveAs(exportFile)
	if errSave != nil {
		log.Println("TPFuncDataWriteTplExcel save data to excel  error:", errSave.Error())
		return true, errSave

	} else {
		return true, nil
	}
}

func UserListWriteTplExcel(data []users.UserModel, templateSheetName, templateFile, newFileName string) (bool, error) {
	//templateFile := "./static/excel_template/template_order.xls"
	exportFile := newFileName

	xlsNew := excelize.NewFile()

	index := xlsNew.NewSheet(templateSheetName)

	//获取模板数据
	//fmt.Println(templateFile)
	axis := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		log.Fatal(err)
	}
	rows := template.GetRows(templateSheetName)
	for i, v := range rows[0] {
		xlsNew.SetCellValue(templateSheetName, axis[i]+strconv.Itoa(1), v)
		xlsNew.SetRowHeight(templateSheetName, 1, 20)
	}

	//第3行开始
	var writingRow = 2
	//var writeErr error

	for _, d := range data {
		//设置行高

		xlsNew.SetRowHeight(templateSheetName, writingRow, 15)

		// ID
		xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.ID)
		// 名称
		xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.UserName)
		// 用户编号
		xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), d.UserNo)
		// 联系人姓名
		xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.Name)
		// 手机号码
		xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), d.Phone)

		var userRelationText string

		if d.IsAdmin == 3 && d.IsPlace == 0 {
			userRelationText = "代理商"
		}

		if d.IsAdmin == 3 && d.IsPlace == 3 {
			userRelationText = "分公司"
		}
		if d.IsAdmin == 3 && d.IsPlace == 1 {
			userRelationText = "商户"
		}
		if d.IsAdmin == 2 {
			userRelationText = "运营商"
		}
		if d.IsAdmin == 4 && d.IsPlace == 0 {
			userRelationText = "员工"
		}
		xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), userRelationText)
		// 加入时间
		xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), time.Unix(int64(d.CreateAt), 0).Format("2006-01-02 15:04"))

		writingRow++
	}

	//设置宽度
	xlsNew.SetColWidth(templateSheetName, "B", "B", 30)
	xlsNew.SetColWidth(templateSheetName, "C", "C", 26)
	xlsNew.SetColWidth(templateSheetName, "E", "E", 15)
	xlsNew.SetColWidth(templateSheetName, "G", "F", 20)

	//设置默认打开的表单
	xlsNew.SetActiveSheet(index)

	// 退出保存文件，退出
	errSave := xlsNew.SaveAs(exportFile)
	if errSave != nil {
		log.Println("TPFuncDataWriteTplExcel save data to excel  error:", errSave.Error())
		return true, errSave

	} else {
		return true, nil
	}
}

//网点评级导出
func PlaceLevelLogExport(data []*placemodel.PlaceLevelLogModel, templateSheetName, templateFile, newFileName string) (bool, error) {

	exportFile := newFileName
	xlsNew := excelize.NewFile()
	excelSheet := xlsNew.NewSheet(templateSheetName)

	//获取模板数据
	axis := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	template, err := excelize.OpenFile(templateFile)
	if err != nil { log.Fatal("", err) }
	rows := template.GetRows(templateSheetName)
	for i, v := range rows[0] {
		xlsNew.SetCellValue(templateSheetName, axis[i]+strconv.Itoa(1), v)
	}
	//第3行开始
	var writingRow = 2
	for _, d := range data {
		//设置行高
		xlsNew.SetRowHeight(templateSheetName, writingRow, 20)
		//网点id
		xlsNew.SetCellValue(templateSheetName, "A"+strconv.Itoa(writingRow), d.PlaceId)
		// 网点名称
		xlsNew.SetCellValue(templateSheetName, "B"+strconv.Itoa(writingRow), d.PlaceName)
		//当前评级
		xlsNew.SetCellValue(templateSheetName, "C"+strconv.Itoa(writingRow), d.Level)
		xlsNew.SetCellValue(templateSheetName, "D"+strconv.Itoa(writingRow), d.Level7)
		xlsNew.SetCellValue(templateSheetName, "E"+strconv.Itoa(writingRow), d.Level15)
		xlsNew.SetCellValue(templateSheetName, "F"+strconv.Itoa(writingRow), d.Level30)
		xlsNew.SetCellValue(templateSheetName, "G"+strconv.Itoa(writingRow), d.Level60)
		xlsNew.SetCellValue(templateSheetName, "H"+strconv.Itoa(writingRow), d.Level90)
		writingRow++
	}

	//设置宽度
	//xlsNew.SetColWidth(templateSheetName, "A", "A", 30)
	xlsNew.SetColWidth(templateSheetName, "B", "B", 30)
	xlsNew.SetColWidth(templateSheetName, "C", "C", 16)
	xlsNew.SetColWidth(templateSheetName, "D", "D", 16)
	xlsNew.SetColWidth(templateSheetName, "E", "E", 16)
	xlsNew.SetColWidth(templateSheetName, "F", "F", 16)
	xlsNew.SetColWidth(templateSheetName, "G", "G", 16)
	xlsNew.SetColWidth(templateSheetName, "H", "H", 16)

	//设置默认打开的表单
	xlsNew.SetActiveSheet(excelSheet)

	// 退出保存文件，退出
	errSave := xlsNew.SaveAs(exportFile)
	if errSave != nil {
		log.Println("TPFuncDataWriteTplExcel save data to excel  error:", errSave.Error())
		return true, errSave

	} else {
		return true, nil
	}
}

func ReadYCExcelInfo(templateSheetName, templateFile string) {

	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		log.Fatal(err)
	}
	var continueOne = 1
	var phones, updateErr string
	var updateCount int
	rows := template.GetRows(templateSheetName)

	for _, rowsValue := range rows {

		if continueOne == 1 {
			continueOne = 0
			continue
		}
		usersInfo := users.UserModel{}
		if rowsValue[3] == "18174716658" || rowsValue[3] == "15178834437" || rowsValue[3] == "15851937426" || rowsValue[3] == "18608419171" || rowsValue[3] == "18218654985" ||
			rowsValue[3] == "15392302111" || rowsValue[3] == "18608651781" || rowsValue[3] == "13430980437" || rowsValue[3] == "18876730240" || rowsValue[3] == "18078196886" {
			updateErr += "直营总部不能修改" + rowsValue[3]
			continue
		}

		if rowsValue[3] != "" {
			db := global.Orm.Debug().Table("users").Where("phone = ?", rowsValue[3]).Find(&usersInfo)
			if db.Error != nil {
				log.Println("查询用户信息失败，手机号为", rowsValue[3])
			}
		}
		var parentId int

		//修改上级
		switch rowsValue[14] {
		case "直营-虚拟":
			parentId = 1058
		case "三明-直营":
			parentId = 848
		case "深圳-直营":
			parentId = 1059
		case "重庆-直营":
			parentId = 402147
		case "湖南-直营":
			parentId = 850
		//case "东呈":
		case "佛山分公司":
			parentId = 217
		case "南宁分公司":
			parentId = 221
		//case "项目":
		case "湖北-直营":
			parentId = 847
		}
		updateModel := users.UserModel{}
		updateModel.ParentID = parentId
		if parentId <= 0 {
			continue
		}
		updateDb := global.Orm.Debug().Table("users").Select("parent_id").Where("id", usersInfo.ID).Model(&updateModel).Updates(&updateModel)
		if updateDb.Error == nil {
			phones += rowsValue[3] + ","
			updateCount++
		} else {
			updateErr = rowsValue[3]
		}

	}
	fmt.Println(phones)
	log.Println("总共修改：", updateCount, updateErr)
}

func ReadZMExcelInfo(templateSheetName, templateFile string) {

	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		log.Fatal(err)
	}
	var continueOne = 1
	var phones, updateErr string
	var updateCount int
	rows := template.GetRows(templateSheetName)

	for _, rowsValue := range rows {

		if continueOne == 1 {
			continueOne = 0
			continue
		}
		usersInfo := users.UserModel{}
		if rowsValue[3] == "18174716658" || rowsValue[3] == "15178834437" || rowsValue[3] == "15851937426" || rowsValue[3] == "18608419171" || rowsValue[3] == "18218654985" ||
			rowsValue[3] == "15392302111" || rowsValue[3] == "18608651781" || rowsValue[3] == "13430980437" || rowsValue[3] == "18876730240" || rowsValue[3] == "18078196886" {
			updateErr += "直营总部不能修改" + rowsValue[3]
			continue
		}

		if rowsValue[3] != "" {
			db := global.Orm.Debug().Table("users").Where("phone = ?", rowsValue[3]).Find(&usersInfo)
			if db.Error != nil {
				log.Println("查询用户信息失败，手机号为", rowsValue[3])
			}
		}
		var parentId int

		//修改上级
		switch rowsValue[13] {
		case "直营-虚拟":
			parentId = 1058
		case "三明-直营":
			parentId = 848
		case "深圳-直营":
			parentId = 1059
		case "重庆-直营":
			parentId = 402147
		case "湖南-直营":
			parentId = 850
		//case "东呈":
		case "佛山分公司":
			parentId = 217
		case "南宁分公司":
			parentId = 221
		case "广西分公司":

		//case "项目":
		case "湖北-直营":
			parentId = 847
		}
		updateModel := users.UserModel{}
		updateModel.ParentID = parentId
		if parentId <= 0 {
			continue
		}
		updateDb := global.Orm.Debug().Table("users").Select("parent_id").Where("id", usersInfo.ID).Model(&updateModel).Updates(&updateModel)
		if updateDb.Error == nil {
			phones += rowsValue[3] + ","
			updateCount++
		} else {
			updateErr = rowsValue[3]
		}

	}
	fmt.Println(phones)
	log.Println("总共修改：", updateCount, updateErr)
}

func OrderDataIsAdminWriteTplExcel(data []*orders.OrderExportModel, templateSheetName, templateFile, newFileName string, index string) (bool, error) {
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		return false, err
	}

	if len(data) > 100000 {
		dataLen := len(data)
		for i := 0; i <= 10; i++ {
			//写入文件数据
			var dataRow []*orders.OrderExportModel
			if dataLen == 0 {
				break
			}
			log.Println("第"+strconv.Itoa(i)+"轮数据，数据长度,", dataLen)
			if dataLen > 100000 {
				dataRow = data[(i * 100000):((i + 1) * 100000)]
				dataLen -= 100000
			} else {
				dataRow = data[(i * 100000):len(data)]
				dataLen = 0
			}

			//设置Excel文件

			file := xlsx.NewFile()
			//设置字体
			xlsx.SetDefaultFont(11, "宋体")
			//xlsx.NewBorder("none", "thin", "none", "thin")
			sheet, err := file.AddSheet(templateSheetName)
			if err != nil {
				return false, err
			}

			//设置标题
			titleRow := sheet.AddRow()
			var rowsTitle []string
			//读取标题值
			rows := template.GetRows(templateSheetName)
			rowsTitle = rows[0]
			var xlsRows []interface{}

			for _, v := range rowsTitle {
				xlsRows = append(xlsRows, v)
			}
			xlsRow := NewRow(titleRow, xlsRows)
			err = xlsRow.SetRowTitle()
			if err != nil {
				return false, err
			}

			for _, v := range dataRow {

				currentRow := sheet.AddRow()

				//tmp := make([]string, 0)
				var addCellValue []interface{}

				addCellValue = append(addCellValue, v.TradeNo)
				addCellValue = append(addCellValue, v.OutTradeNo)

				var statusText = ""
				//订单状态
				switch v.State {
				case 1:
					statusText = "等待下单"
				case 2:
					statusText = "使用中"
				case 3:
					statusText = "已撤销"
				case 4:
					statusText = "交易失败"
				case 5:
					statusText = "已完成"
				case 6:
					statusText = "支付超时"
				case 7:
					statusText = "待守约"
				case 10:
					statusText = "已删除"

				}

				addCellValue = append(addCellValue, v.MemberName)
				addCellValue = append(addCellValue, statusText)

				//订单来源
				var orderFromStr = ""
				if v.OrderFrom == "weixin" {
					orderFromStr = "微信"
				} else {
					orderFromStr = "支付宝"
				}

				payMoney, _ := decimal.NewFromInt(v.PayMoney).Div(decimal.NewFromInt(100)).Float64()
				settlementMoney, _ := decimal.NewFromInt(v.SettlementMoney).Div(decimal.NewFromInt(100)).Float64()
				couponMoney, _ := decimal.NewFromInt(v.CouponMoney).Div(decimal.NewFromInt(100)).Float64()
				//
				//payMoney := decimal.NewFromInt(v.PayMoney).Div(decimal.NewFromInt(100)).StringFixed(2)
				//settlementMoney := decimal.NewFromInt(v.SettlementMoney).Div(decimal.NewFromInt(100)).StringFixed(2)
				//payMoney := decimal.NewFromInt(v.CouponMoney).Div(decimal.NewFromInt(100)).StringFixed(2)

				addCellValue = append(addCellValue, orderFromStr)
				addCellValue = append(addCellValue, v.HotelId)
				addCellValue = append(addCellValue, v.PlaceId)
				addCellValue = append(addCellValue, v.PlaceName)
				addCellValue = append(addCellValue, v.DeviceSn)
				addCellValue = append(addCellValue, payMoney)
				addCellValue = append(addCellValue, settlementMoney)
				addCellValue = append(addCellValue, v.TradeStateDesc)
				var IsCoupon = ""
				if v.CouponType == 1 {
					IsCoupon = "是"
				} else {
					IsCoupon = "否"
				}
				addCellValue = append(addCellValue, IsCoupon)
				addCellValue = append(addCellValue, couponMoney)
				addCellValue = append(addCellValue, time.Unix(int64(v.CreateAt), 0).Format("2006-01-02 15:04"))
				addCellValue = append(addCellValue, time.Unix(int64(v.EndAt), 0).Format("2006-01-02 15:04"))

				var IsDrawText = ""
				var IsTianHaiText = ""

				if v.IsDraw == 1 {
					IsDrawText = "是"
				} else {

					IsDrawText = "否"
				}

				if v.IsTianHai != 0 {
					IsTianHaiText = "是"
				} else {
					IsTianHaiText = "否"
				}

				addCellValue = append(addCellValue, IsDrawText)

				addCellValue = append(addCellValue, IsTianHaiText)

				xlsRow := NewRow(currentRow, addCellValue)
				err := xlsRow.GenerateRow()
				if err != nil {
					return false, err
				}
			}
			//设置宽度
			sheet.Cols[0].Width = 28
			sheet.Cols[1].Width = 30
			sheet.Cols[7].Width = 20
			sheet.Cols[8].Width = 20
			sheet.Cols[11].Width = 20
			sheet.Cols[14].Width = 20
			sheet.Cols[15].Width = 20

			err = file.Save(newFileName + "/orders(" + strconv.Itoa(i) + ").xlsx")
			if err != nil {
				return false, err
			}
		}

	}

	return true, nil
}

func UserTotalWriteTplExport(data []*agentincome.AgentIncomeModel, templateSheetName, newFileName string) (bool, error) {

	file := xlsx.NewFile()
	//设置字体
	xlsx.SetDefaultFont(11, "宋体")
	sheet, err := file.AddSheet(templateSheetName)
	if err != nil {
		return false, err
	}
	//设置标题
	titleRow := sheet.AddRow()

	//代理商ID	代理商名称	联系人	电话	网点数	完成订单数	撤销订单数	结算金额	分红收益	统计日期
	xlsRows := []interface{}{"代理商ID", "代理商名称", "联系人", "电话", "网点数", "完成订单数", "撤销订单数", "结算金额", "分红收益", "统计日期"}

	xlsRow := NewRow(titleRow, xlsRows)
	err = xlsRow.SetRowTitle()
	if err != nil {
		return false, err
	}

	for _, v := range data {
		currentRow := sheet.AddRow()
		divindsMoney, _ := decimal.NewFromInt(v.DivindsMoney).Div(decimal.NewFromInt(100)).Float64()
		tradeTotal, _ := decimal.NewFromInt(v.TradeTotal).Div(decimal.NewFromInt(100)).Float64()
		//tmp := make([]string, 0)
		var addCellValue []interface{}
		addCellValue = append(addCellValue, v.AgentUserId)
		addCellValue = append(addCellValue, v.AgentUserName)
		addCellValue = append(addCellValue, v.Name)
		addCellValue = append(addCellValue, v.Phone)
		addCellValue = append(addCellValue, v.PlacesTotal)
		addCellValue = append(addCellValue, v.CompleteCount)
		addCellValue = append(addCellValue, v.CancelCount)
		addCellValue = append(addCellValue, tradeTotal)
		addCellValue = append(addCellValue, divindsMoney)
		addCellValue = append(addCellValue, v.IncomeDay)

		xlsRow := NewRow(currentRow, addCellValue)
		err := xlsRow.GenerateRow()
		if err != nil {
			return false, err
		}
	}
	//设置宽度

	sheet.Cols[1].Width = 25
	sheet.Cols[2].Width = 25
	sheet.Cols[3].Width = 15
	sheet.Cols[9].Width = 15
	err = file.Save(newFileName)
	if err != nil {
		return false, err
	}
	return true, nil
}

func BasicAnalysisDataExport(data []*statistics.StatisticsModel, templateSheetName, templateFile, newFileName string) (bool, error) {
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		return false, err
	}

	file := xlsx.NewFile()
	//设置字体
	xlsx.SetDefaultFont(11, "宋体")
	sheet, err := file.AddSheet(templateSheetName)
	if err != nil {
		return false, err
	}

	titleRow := sheet.AddRow()
	var rowsTitle []string
	rows := template.GetRows(templateSheetName)
	rowsTitle = rows[0][0 : len(rows[0])-2]
	var xlsRows []interface{}
	for _, v := range rowsTitle { xlsRows = append(xlsRows, v) }
	xlsRow := NewRow(titleRow, xlsRows)
	err = xlsRow.SetRowTitle()
	if err != nil { return false, err }

	for _, v := range data {
		currentRow := sheet.AddRow()
		var addCellValue []interface{}
		addCellValue = append(addCellValue, decimal.NewFromInt(v.SettlementTotal).Div(decimal.NewFromInt(100)).StringFixed(2)) // 流水
		addCellValue = append(addCellValue, v.CountEnd) // 笔数
		priceDecimal := decimal.NewFromInt(v.SettlementTotal).Div(decimal.NewFromInt(100))
		if v.CountEnd > 0 {
			price := priceDecimal.Div(decimal.NewFromInt(v.CountEnd))
			addCellValue = append(addCellValue, price.StringFixed(2)) // 单价
		} else {
			addCellValue = append(addCellValue, 0) // 单价
		}

		addCellValue = append(addCellValue, v.Places) // 门店数
		if v.Places > 0 {
			addCellValue = append(addCellValue, decimal.NewFromInt(v.CountPlace).Div(decimal.NewFromInt(v.Places)).StringFixed(2)) // 门店动销
		} else {
			addCellValue = append(addCellValue, 0) // 门店动销
		}
		addCellValue = append(addCellValue, v.Devices) // 设备数
		if v.CountDevice > 0 {
			addCellValue = append(addCellValue, decimal.NewFromInt(v.CountDevice).Div(decimal.NewFromInt(v.Devices)).StringFixed(2)) // 设备动销
		} else {
			addCellValue = append(addCellValue, 0) // 设备动销
		}

		addCellValue = append(addCellValue, decimal.NewFromInt(v.WeixinTotal).Add(decimal.NewFromInt(v.QrcodeSuccessTotal)).IntPart()) // 提现笔数
		cashMoney := decimal.NewFromInt(v.WeixinMoney).Add(decimal.NewFromInt(v.QrcodeSuccessMoney))
		addCellValue = append(addCellValue, cashMoney.Div(decimal.NewFromInt(100)).IntPart()) // 提现金额

		addCellValue = append(addCellValue, decimal.NewFromInt(v.SettlementAlldraw).Div(decimal.NewFromInt(100)).StringFixed(2)) // 抽单流水
		addCellValue = append(addCellValue, decimal.NewFromInt(v.SettlementSmalldraw).Div(decimal.NewFromInt(100)).StringFixed(2)) // 奖励流水

		addCellValue = append(addCellValue, decimal.NewFromInt(v.IncomeTotal).Div(decimal.NewFromInt(100)).StringFixed(2)) // 总收益
		addCellValue = append(addCellValue, decimal.NewFromInt(v.IncomeBusiness).Div(decimal.NewFromInt(100)).StringFixed(2)) // 运营收益
		addCellValue = append(addCellValue, decimal.NewFromInt(v.IncomeBranch).Div(decimal.NewFromInt(100)).StringFixed(2)) // 分公司收益

		xlsRow := NewRow(currentRow, addCellValue)
		err := xlsRow.GenerateRow()
		if err != nil {
			return false, err
		}
	}

	//设置宽度
	//sheet.Cols[0].Width = 28
	//sheet.Cols[1].Width = 30
	//sheet.Cols[7].Width = 20
	//sheet.Cols[8].Width = 20
	//sheet.Cols[11].Width = 20
	//sheet.Cols[14].Width = 20
	//sheet.Cols[15].Width = 20

	err = file.Save(newFileName)
	if err != nil {
		return false, err
	}
	return true, nil
}


func AnalysisUserDataExport(data []*statistics.StatisticsUserList, templateSheetName, templateFile, newFileName string) (bool, error) {
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		return false, err
	}

	file := xlsx.NewFile()
	//设置字体
	xlsx.SetDefaultFont(11, "宋体")
	sheet, err := file.AddSheet(templateSheetName)
	if err != nil {
		return false, err
	}

	titleRow := sheet.AddRow()
	var rowsTitle []string
	rows := template.GetRows(templateSheetName)
	rowsTitle = rows[0][0 : len(rows[0])-2]
	var xlsRows []interface{}
	for _, v := range rowsTitle { xlsRows = append(xlsRows, v) }
	xlsRow := NewRow(titleRow, xlsRows)
	err = xlsRow.SetRowTitle()
	if err != nil { return false, err }

	for _, v := range data {
		currentRow := sheet.AddRow()
		var addCellValue []interface{}
		addCellValue = append(addCellValue, v.DateStr)
		addCellValue = append(addCellValue, strconv.Itoa(int(v.UserId)) + "/" + v.UserName)
		addCellValue = append(addCellValue, v.Name)
		addCellValue = append(addCellValue, v.Phone)
		addCellValue = append(addCellValue, decimal.NewFromInt(v.SettlementNodraw).Div(decimal.NewFromInt(100)).StringFixed(2)) // 流水
		addCellValue = append(addCellValue, decimal.NewFromInt(v.TotalIncome).Div(decimal.NewFromInt(100)).StringFixed(2)) // 流水
		addCellValue = append(addCellValue, v.TotalTeam)
		addCellValue = append(addCellValue, v.TotalDirectTeam)
		addCellValue = append(addCellValue, v.TotalPlace)
		addCellValue = append(addCellValue, v.TotalDirectPlace)
		addCellValue = append(addCellValue, v.TotalDevices)

		addCellValue = append(addCellValue, decimal.NewFromInt(v.WeixinTotal).Add(decimal.NewFromInt(v.QrcodeSuccessTotal)).IntPart()) // 提现笔数
		cashMoney := decimal.NewFromInt(v.WeixinMoney).Add(decimal.NewFromInt(v.QrcodeSuccessMoney))
		addCellValue = append(addCellValue, cashMoney.Div(decimal.NewFromInt(100)).IntPart()) // 提现金额

		addCellValue = append(addCellValue, decimal.NewFromInt(v.SettlementTotal).Div(decimal.NewFromInt(100)).StringFixed(2)) // 流水
		addCellValue = append(addCellValue, decimal.NewFromInt(v.SettlementDraw).Div(decimal.NewFromInt(100)).StringFixed(2)) // 流水
		addCellValue = append(addCellValue, decimal.NewFromInt(v.SettlementSmalldraw).Div(decimal.NewFromInt(100)).StringFixed(2)) // 流水

		xlsRow := NewRow(currentRow, addCellValue)
		err := xlsRow.GenerateRow()
		if err != nil {
			return false, err
		}
	}

	//设置宽度
	//sheet.Cols[0].Width = 28
	sheet.Cols[1].Width = 30
	sheet.Cols[2].Width = 20
	sheet.Cols[3].Width = 15
	//sheet.Cols[7].Width = 20
	//sheet.Cols[8].Width = 20
	//sheet.Cols[11].Width = 20
	//sheet.Cols[14].Width = 20
	//sheet.Cols[15].Width = 20

	err = file.Save(newFileName)
	if err != nil {
		return false, err
	}
	return true, nil
}

// 合伙人数据导出
func ExportPartnerUserAnalysisList(data []*statistics.StatisticsUserList, templateSheetName, templateFile, newFileName string) (bool, error) {
	template, err := excelize.OpenFile(templateFile)
	if err != nil {
		return false, err
	}

	file := xlsx.NewFile()
	//设置字体
	xlsx.SetDefaultFont(11, "宋体")
	sheet, err := file.AddSheet(templateSheetName)
	if err != nil {
		return false, err
	}

	titleRow := sheet.AddRow()
	var rowsTitle []string
	rows := template.GetRows(templateSheetName)
	rowsTitle = rows[0][0 : len(rows[0])-2]
	var xlsRows []interface{}
	for _, v := range rowsTitle { xlsRows = append(xlsRows, v) }
	xlsRow := NewRow(titleRow, xlsRows)
	err = xlsRow.SetRowTitle()
	if err != nil { return false, err }

	for _, v := range data {
		currentRow := sheet.AddRow()
		var addCellValue []interface{}
		addCellValue = append(addCellValue, v.DateStr)
		addCellValue = append(addCellValue, v.UserName)
		addCellValue = append(addCellValue, v.Name)
		addCellValue = append(addCellValue, v.Phone)
		addCellValue = append(addCellValue, v.Province)
		addCellValue = append(addCellValue, v.City)
		addCellValue = append(addCellValue, v.TotalTeam)
		addCellValue = append(addCellValue, v.TotalPlace)
		addCellValue = append(addCellValue, v.PlaceSales)
		addCellValue = append(addCellValue, decimal.NewFromInt(v.SettlementComplete).Div(decimal.NewFromInt(100)).StringFixed(2))
		addCellValue = append(addCellValue, v.TotalComplete)
		addCellValue = append(addCellValue, decimal.NewFromInt(v.TotalIncome).Div(decimal.NewFromInt(100)).StringFixed(2))
		if v.TotalComplete > 0 {
			addCellValue = append(addCellValue, decimal.NewFromInt(v.TotalIncome).Div(decimal.NewFromInt(100)).Div(decimal.NewFromInt(v.TotalComplete)).StringFixed(2))
		} else {
			addCellValue = append(addCellValue, 0)
		}

		addCellValue = append(addCellValue, decimal.NewFromInt(v.SettlementDraw).Div(decimal.NewFromInt(100)).StringFixed(2))

		xlsRow := NewRow(currentRow, addCellValue)
		err := xlsRow.GenerateRow()
		if err != nil {
			return false, err
		}
	}

	//设置宽度
	//sheet.Cols[0].Width = 28
	sheet.Cols[1].Width = 30
	sheet.Cols[2].Width = 20
	sheet.Cols[3].Width = 15
	//sheet.Cols[7].Width = 20
	//sheet.Cols[8].Width = 20
	//sheet.Cols[11].Width = 20
	//sheet.Cols[14].Width = 20
	//sheet.Cols[15].Width = 20

	err = file.Save(newFileName)
	if err != nil {
		return false, err
	}
	return true, nil
}


// 订单列表 - 客服
func OrderListKfExport(lists []*orders.OrderList, templateFile string) error {
	//创建文件
	f, err := os.Create(templateFile)
	if err != nil {
		return err
	}
	defer f.Close()
	// 写入UTF-8 BOM
	f.WriteString("\xEF\xBB\xBF")
	//创建一个新的写入文件流
	w := csv.NewWriter(f)
	orderFromMap := make(map[string]string, 0)
	orderFromMap["weixin"] = "微信"
	orderFromMap["zhifubao"] = "支付宝"

	statusMap := make(map[int8]string, 0)
	statusMap[1] = "等待下单"
	statusMap[2] = "服务中"
	statusMap[3] = "已撤销"
	statusMap[4] = "交易失败"
	statusMap[5] = "已归还"
	statusMap[6] = "支付超时"
	statusMap[7] = "待守约"
	statusMap[8] = "已恢复"
	statusMap[10] = "已删除"
	statusMap[11] = "退款"


	w.Write([]string{"订单编号", "会员名称", "订单状态",
		"订单来源", "携程母酒店ID", "中曼酒店ID", "商户名称", "设备标识", "支付金额", "结算金额", "借出时间", "归还时间"})

	for _, lst := range lists {
		t1 := time.Unix(int64(lst.CreateAt), 0)
		createStr := t1.Format("2006-01-02 15:04:05")
		t2 := time.Unix(int64(lst.EndAt), 0)
		endStr := t2.Format("2006-01-02 15:04:05")
		w.Write([]string{lst.TradeNo, lst.MemberName, statusMap[lst.State], orderFromMap[lst.OrderFrom],
			lst.HotelId, strconv.Itoa(lst.PlaceId), lst.PlaceName, lst.DeviceSn,
			decimal.NewFromInt(lst.PayMoney).Div(decimal.NewFromInt(100)).StringFixed(2),
			decimal.NewFromInt(lst.SettlementMoney).Div(decimal.NewFromInt(100)).StringFixed(2), createStr, endStr })
	}

	w.Flush()
	return nil
}

// 客户扫码日志
func ScanLogExport(lists []scanlogs.ScanLogsModel, templateFile string) error {
	//创建文件
	f, err := os.Create(templateFile)
	if err != nil {
		return err
	}
	defer f.Close()
	// 写入UTF-8 BOM
	f.WriteString("\xEF\xBB\xBF")
	//创建一个新的写入文件流
	w := csv.NewWriter(f)

	w.Write([]string{"门店ID", "门店名称", "设备编号", "openid", "路径", "场景值", "描述", "ip", "创建时间"})

	for _, lst := range lists {
		t := time.Unix(int64(lst.CreateAt), 0)
		//返回string
		dateStr := t.Format("2006-01-02 15:04:05")
		w.Write([]string{strconv.Itoa(lst.PlaceId), lst.PlaceName, lst.DeviceSn, lst.Openid,
			lst.PagePath, lst.SenceId, lst.SenceDesc, lst.ViewIp, dateStr })
	}

	w.Flush()
	return nil
}

// 服务点击日志
func ServiceClickExport(lists []liveservicelog.ServiceLogList, templateFile string) error {
	//创建文件
	f, err := os.Create(templateFile)
	if err != nil {
		return err
	}
	defer f.Close()
	// 写入UTF-8 BOM
	f.WriteString("\xEF\xBB\xBF")
	//创建一个新的写入文件流
	w := csv.NewWriter(f)

	w.Write([]string{"门店ID", "门店名称", "设备号","用户手机", "城市", "服务类型", "服务描述", "创建时间"})
	serviceClickMap := make(map[int]string, 0)
	serviceClickMap[0] = "其他"
	serviceClickMap[1] = "清扫服务"
	serviceClickMap[2] = "客房送物"
	serviceClickMap[3] = "维修服务"
	serviceClickMap[4] = "联系前台"
	serviceClickMap[5] = "预约开票"
	serviceClickMap[6] = "续住服务"
	serviceClickMap[7] = "在线退房"
	serviceClickMap[8] = "电子早餐券"
	serviceClickMap[9] = "好评有礼"
	serviceClickMap[10] = "投诉建议"
	serviceClickMap[11] = "酒店超市"
	serviceClickMap[12] = "叫醒服务"
	serviceClickMap[13] = "售卖服务"
	serviceClickMap[88] = "手机充电"

	for _, lst := range lists {
		t := time.Unix(int64(lst.CreateAt), 0)
		//返回string
		dateStr := t.Format("2006-01-02 15:04:05")
		w.Write([]string{strconv.Itoa(lst.PlaceId), lst.PlaceName, lst.DeviceSn, lst.Phone,
			lst.Province + lst.City + lst.County, serviceClickMap[lst.ServiceType], lst.ServiceDesc, dateStr })
	}

	w.Flush()
	return nil
}